﻿using NLog;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;

namespace uTrade.Common
{
    public static class uLog
    {
        static private NLog.Config.LoggingConfiguration cfg = null;

        static private Logger _logger
        {
            get
            {
                //_logger = LogManager.GetCurrentClassLogger();
                if (cfg == null)
                {
                    cfg = new NLog.Config.LoggingConfiguration();
                    cfg.LoggingRules.Add(new NLog.Config.LoggingRule("log", LogLevel.Info, new NLog.Targets.ColoredConsoleTarget
                    {
                        Name = "console",
                        Layout = "${longdate} ${message}",
                        Encoding = Encoding.UTF8,
                        //Layout = "[${date:format=yyyyMMddHH\:mm\:ss}][${level}] ${message}  ${message}",
                    }));

                    cfg.LoggingRules.Add(new NLog.Config.LoggingRule("log", LogLevel.Info, new NLog.Targets.FileTarget
                    {
                        Name = "log",
                        FileName = "${basedir}/log/${date:format=yyyyMMdd}.log",
                        Layout = "${longtdate} ${message}",
                        Encoding = Encoding.UTF8,
                        //Layout = "[${date:format=yyyyMMddHH\:mm\:ss}][${level}] ${message}  ${message}",
                    }));
                    LogManager.Configuration = cfg;
                }
                return LogManager.GetLogger("log");
            }
        }

        static public void Log(string pMsg, LogLevel pLevel = null, [CallerMemberName] string pCallName = "", [CallerLineNumber] int pLineNumber = 0, [CallerFilePathAttribute] string pFileName = "")
        {
            _logger.Log(pLevel ?? LogLevel.Debug, "{0,4}|{1}|{2}=> {3}", pCallName, new FileInfo(pFileName).Name, pLineNumber, pMsg);
        }

        static public void LogTrace(string pMsg, [CallerMemberName] string pCallName = "", [CallerLineNumber] int pLineNumber = 0, [CallerFilePathAttribute] string pFileName = "")
        {
            _logger.Log(LogLevel.Trace, "{0,4}|{1}|{2}=> {3}", pCallName, new FileInfo(pFileName).Name, pLineNumber, pMsg);
        }

        static public void LogInfo(string pMsg, [CallerMemberName] string pCallName = "", [CallerLineNumber] int pLineNumber = 0, [CallerFilePathAttribute] string pFileName = "")
        {
            _logger.Log(LogLevel.Info, "{0,4}|{1}|{2}=> {3}", pCallName, new FileInfo(pFileName).Name, pLineNumber, pMsg);
        }

        static public void LogDebug(string pMsg, [CallerMemberName] string pCallName = "", [CallerLineNumber] int pLineNumber = 0, [CallerFilePathAttribute] string pFileName = "")
        {
            _logger.Log(LogLevel.Debug, "{0,4}|{1}|{2}=> {3}", pCallName, new FileInfo(pFileName).Name, pLineNumber, pMsg);
        }

        static public void LogWarn(string pMsg, [CallerMemberName] string pCallName = "", [CallerLineNumber] int pLineNumber = 0, [CallerFilePathAttribute] string pFileName = "")
        {
            _logger.Log(LogLevel.Warn, "{0,4}|{1}|{2}=> {3}", pCallName, new FileInfo(pFileName).Name, pLineNumber, pMsg);
        }

        static public void LogError(string pMsg, [CallerMemberName] string pCallName = "", [CallerLineNumber] int pLineNumber = 0, [CallerFilePathAttribute] string pFileName = "")
        {
            _logger.Log(LogLevel.Error, "{0,4}|{1}|{2}=> {3}", pCallName, new FileInfo(pFileName).Name, pLineNumber, pMsg);
            if (File.Exists("Alert.wav"))
                new System.Media.SoundPlayer("Alert.wav").Play();
        }
    }
}